package com.tledu.production.controller.crafts;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.constant.CommonConstant;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.crafts.TechnologyModel;
import com.tledu.production.model.crafts.TechnologyPlanModel;
import com.tledu.production.model.crafts.TechnologyRequirementModel;
import com.tledu.production.service.crafts.ITechnologyReallyPlanService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/technologyReally")
public class TechnologyReallyPlanController {
    private final ITechnologyReallyPlanService technologyReallyPlanService;
    @RequestMapping("pageReally")
    @ResponseBody
    public AjaxResult<PageInfo<TechnologyPlanModel>>page(HttpServletRequest req, Integer pageNum, Integer pageSize, String condition, String conditionName){
        if (pageNum == null)pageNum = CommonConstant.PAGENUM;
        if (pageSize == null)pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum,pageSize);
        List<TechnologyPlanModel> list = null;
        PageInfo<TechnologyPlanModel> page = null;
        //分页
        if (conditionName==null||"".equals(conditionName)){
            list= technologyReallyPlanService.list();
            page= PageInfo.of(list);
        }else{
            //搜索
            list= technologyReallyPlanService.selectByName(condition,conditionName);
            page=PageInfo.of(list);
        }
        return new AjaxResult<>(page);
    }
    //删除
    @RequestMapping("delrel")
    public AjaxResult<String> delplan(String technologyPlanId){
        technologyReallyPlanService.del(technologyPlanId);
        return new AjaxResult<>("删除成功");
    }
    //批量删除
    @GetMapping("/delrelAll")
    @ResponseBody
    public AjaxResult batchDel(String ids){
        String[] idsarr = ids.split(",");
        int i =0;
        for(String s : idsarr){
            technologyReallyPlanService.del(s);
            i++;
        }
        if (i >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }
    //增加
    @RequestMapping("/insertRel")
    @ResponseBody
    public AjaxResult<Void>add(TechnologyPlanModel technologyPlanModel){
        technologyReallyPlanService.insert(technologyPlanModel);
        return new AjaxResult<>();
    }
    //编辑

    @RequestMapping("/update")
    @ResponseBody
    public AjaxResult<Void>update(TechnologyPlanModel technologyPlanModel){

        technologyReallyPlanService.update(technologyPlanModel);
        return new AjaxResult<>();
    }
}
